package com.example.controller;

import com.example.annotation.RecordUserLog;
import com.example.config.UserRealm;
import com.example.exception.CaptchaException;
import com.example.pojo.User;
import com.sun.deploy.net.HttpResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author code威
 * @version 1.0
 * @description: TODO
 * @date 2021/12/5 20:39
 */
@Controller
public class MyController {


    private static final transient Logger log = LoggerFactory.getLogger(MyController.class);

    @RequestMapping({"/","/index"})
    public String toIndex(Model model){
        model.addAttribute("msg","hello,Shiro");
        return "index";
    }

    @RequestMapping("/user/add")
    public String add(){
        return "user/add";
    }

    @RequestMapping("/user/update")
    public String update(){
        return "user/update";
    }

    @RequestMapping("/tologin")
    @RecordUserLog(userName = "zw",operate = "登录操作")
    public String toLogin(Model model){

        return "login";
    }

    @RequestMapping("/login")
    @RecordUserLog(userName = "zw",operate = "登录认证")
    public String login(String username,String password,String remeberme,Model model){
        log.info("登录请求开始");
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken userToken = new UsernamePasswordToken(username,password);

        try {
            subject.login(userToken);//在userReaml类完成认证操作
        } catch (UnknownAccountException e) {//用户名不存在

            model.addAttribute("msg","用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        } catch (AuthenticationException e){
            model.addAttribute("msg","验证码错误");
            return "login";

        }
        //根据前端设置记住我的功能
        if (StringUtils.isNotBlank(remeberme)){
            userToken.setRememberMe(true);
        }
        return "index";

    }

    @RequestMapping("/Unauthorized")
    @ResponseBody
    @RecordUserLog(userName = "zw",operate = "登录失败")
    public String Unauthorized(){

        return "对不起，您暂时未有权限访问";
    }

    @RequestMapping("/logout")
    @RecordUserLog(userName = "zw",operate = "登出操作")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.stop();
        subject.logout();
        response.sendRedirect("/tologin");
    }

    /***
     * @description: 锁屏
     * @author code威
     * @date 2022/1/8 11:07
     * @version 1.0
     */
    @RequestMapping("/lock")
    @RecordUserLog(userName = "zw",operate = "锁屏操作")
    public void lock(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User) subject.getPrincipal();
        session.setAttribute("user",user);
        session.setAttribute("lockscreen",true);
    }




}
